/*****************************************************************************
 *
 * HUSONG PROPRIETARY INFORMATION
 *
 * The information contained herein is proprietary to HUSONG
 * and shall not be reproduced or disclosed in whole or in part
 * or used for any design or manufacture
 * without direct written authorization from HUSONG.
 *
 * Copyright (c) 2015 by HUSONG. All rights reserved.
 *
 *****************************************************************************/
package com.broker.provider.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.send.broker.entity.Broker;
import me.send.broker.entity.Feedback;
import me.send.broker.entity.TrainPic;
import me.send.broker.entity.base.ReturnBean;
import me.send.broker.entity.base.TrainTest;
import me.send.broker.pojo.BrokerIndexBean;
import me.send.broker.service.BrokerService;
import me.send.business.base.BaseSMS;
import me.send.business.base.openAccount.AccountOperate;
import me.send.framework.util.CacheHelper;
import me.send.framework.util.CacheUtil;
import me.send.framework.util.HelperUtil;
import me.send.framework.util.MD5Util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.broker.provider.base.BaseService;
import com.broker.provider.cons.BrokerCons;

/**
 * @author maxmin
 *
 * @date
 */
public class BrokerServiceImpl extends BaseService implements BrokerService {
	private final static Logger logger = LoggerFactory
			.getLogger(BrokerServiceImpl.class.getName());

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.BrokerService#update_username(int,
	 * java.lang.String, java.lang.String)
	 */
	public int update_username(int popid, String username, String devicenumber) {
		if (popid > 0 && username != null && !username.equals("")) {
			Broker popuser = new Broker();
			popuser.setUsername(username);
			popuser.setUpdatetime(HelperUtil.getCurrentDate());
			popuser.setPopid(popid);
			return brokerMapper.update_username(popuser);

		} else {
			return -1;// 参数错误
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.BrokerService#update_headerpath(int,
	 * java.lang.String, java.lang.String)
	 */
	public int update_headerpath(int popid, String headerpath,
			String devicenumber) {
		// 验证这个 devicenumber
		if (popid > 0 && headerpath != null && !headerpath.equals("")) {
			Broker popuser = new Broker();
			popuser.setPopid(popid);
			popuser.setUpdatetime(HelperUtil.getCurrentDate());
			popuser.setHeaderpath(headerpath);
			return brokerMapper.update_headerpath(popuser);

		} else {
			return -1;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.BrokerService#update_sex(int, int,
	 * java.lang.String)
	 */
	public int update_sex(int popid, int sex, String devicenumber) {

		if (popid > 0 && sex > 0) {
			Broker popuser = new Broker();
			popuser.setPopid(popid);
			popuser.setUpdatetime(HelperUtil.getCurrentDate());
			popuser.setSex(sex);
			return brokerMapper.update_sex(popuser);

		} else {
			return -1;
		}

	}
	
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.send.broker.service.BrokerService#update_password(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	public int update_password(String telephone, String password,String code,
			String devicenumber) {

		if (telephone != null && !telephone.equals("") && password != null
				&& !password.equals("")) {
			CacheUtil cacheUtil = CacheUtil.getInstance();
			if (cacheUtil.get(telephone) == null) {
				return -2;
			}
			if (!cacheUtil.get(telephone).equals(code)) {
				return -3;
			}
			Broker popuser = new Broker();
			popuser.setPassword(password);
			popuser.setUpdatetime(HelperUtil.getCurrentDate());
			popuser.setTelephone(telephone);
			return brokerMapper.update_password(popuser);

		} else {
			return -1;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.BrokerService#update_user(java.lang.String,
	 * me.send.broker.entity.Broker)
	 */
	public ReturnBean update_user(String devicenumber, Broker user) {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.send.broker.service.BrokerService#updatePassword(java.lang.String,
	 * java.lang.String, java.lang.String, java.lang.String)
	 */
	public int updatePassword(String telephone, String devicenumber,
			String code, String password) {

		if (telephone != null && !telephone.equals("") && password != null
				&& !"".equals(password)) {
			// 调用相关的短信接口
			// telephone = Base64Util.getFromBase64(telephone);
			// 判断验证码
			if (CacheHelper.get(telephone) == null) {
				return -2;
			}
			if (!CacheHelper.get(telephone).equals(code)) {
				return -3;
			}
			Broker user = new Broker();
			user.setUpdatetime(HelperUtil.getCurrentDate());
			user.setTelephone(telephone);
			user.setPassword(password);
			return brokerMapper.update_password(user);

		} else {
			return -1;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.BrokerService#sendCode(java.lang.String,
	 * java.lang.String)
	 */
	public ReturnBean sendCode(String telephone, String devicenumber) {
		ReturnBean rb = new ReturnBean();
		try {
			if (!isDevicenumber(devicenumber)) {
				rb.setCode(BrokerCons.VALIDATE_FAIL);
				rb.setMessage("验证失败");
			}else {
				if (telephone != null && !telephone.equals("")) {
					int x = (int) (Math.random() * 9000 + 1000);
					BaseSMS.getMessage(telephone, x + "");
					// 赋值开始时间 2015年5月27日10:57:16
					int exp = 180;
					// boolean replace = CacheUtil.replace(telephone, exp, x + "");
					CacheUtil cacheUtil = CacheUtil.getInstance();
					boolean replace = cacheUtil.set(telephone, exp, x + "");
					System.out.println("cache result ：" + replace);
					System.out.println("cache code: " + cacheUtil.get(telephone));
					rb.setCode(BrokerCons.LOGIN_SUCCESS);
					rb.setMessage(ReturnBean.SUCCESS_STR);
				} else {
					rb.setCode(BrokerCons.VALIDATE_FAIL);
					rb.setMessage("验证失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.BrokerService#verification(java.lang.String,
	 * java.lang.String)
	 */
	public ReturnBean verification(String telephone, String devicenumber) {
		ReturnBean rb = new ReturnBean();
		if (isDevicenumber(devicenumber)) {
			if (brokerMapper.count_telephone(telephone) > 0) {
				rb.setCode(BrokerCons.VALIDATE_FAIL);
				rb.setMessage("账号已存在");
				return rb;
			}
			rb.setCode(BrokerCons.LOGIN_SUCCESS);
			rb.setMessage(ReturnBean.SUCCESS_STR);
			return rb;
		}
		rb.setCode(BrokerCons.VALIDATE_FAIL);
		rb.setMessage("验证失败");
		return rb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.send.broker.service.BrokerService#login(me.send.broker.entity.Broker,
	 * java.lang.String)
	 */
	public ReturnBean login(Broker user, String devicenumber) {
		ReturnBean returnBean = new ReturnBean();
		if (isDevicenumber(devicenumber)) {
			Broker broker = brokerMapper.login(user);
			if (broker != null) {
				List<Broker> list = new ArrayList<Broker>();
				list.add(broker);
				returnBean.setDatas(list);
				returnBean.setCode(BrokerCons.LOGIN_SUCCESS);
				returnBean.setMessage(ReturnBean.SUCCESS_STR);
			} else {
				returnBean.setCode(BrokerCons.USERNAME_OR_PASSWORD_ERROR);
				returnBean.setMessage("账号或密码错误");
			}
		} else {
			returnBean.setCode(BrokerCons.VALIDATE_FAIL);
			returnBean.setMessage("验证失败");
		}
		return returnBean;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.send.broker.service.BrokerService#register(java.lang.String,
	 * java.lang.String, java.lang.String, java.lang.String)
	 */
	public ReturnBean register(String telephone, String devicenumber,
			String code, String password,String bindmac,String cityname) {
		ReturnBean rb = new ReturnBean();
		try {
			if (isDevicenumber(devicenumber)) {
				// 调用相关的短信接口
				// telephone = Base64Util.getFromBase64(telephone);
				// 判断验证码
				if ((telephone != null) && (!telephone.equals(""))
						&& (password != null) && (!"".equals(password))) {
					// 调用相关的短信接口
					// telephone = Base64Util.getFromBase64(telephone);
					// 判断验证码
					CacheUtil cacheUtil = CacheUtil.getInstance();
					if (cacheUtil.get(telephone) == null) {
						rb.setCode(BrokerCons.VALIDATE_FAIL);
						rb.setMessage("验证码不能为空");
						return rb;
					}
					if (!cacheUtil.get(telephone).equals(code)) {
						rb.setCode(BrokerCons.VALIDATE_FAIL);
						rb.setMessage("验证失败,验证码不正确");
						return rb;
					}
					Broker user = new Broker();
					user.setCreatetime(HelperUtil.getCurrentDate());
					// 使用uuid的10位 做用户名
					user.setUsername(telephone.substring(1, 6)
							+ HelperUtil.uuid().substring(0, 5));
					// 设置 电话号码 密码 最后注册
					user.setTelephone(telephone);
					user.setPassword(password);
					user.setBindmac(bindmac);
					user.setCityname(cityname);

					// 注册阿里云帐号
					AccountOperate operate = new AccountOperate();
					String id = MD5Util.MD5(devicenumber
							+ System.currentTimeMillis()+Math.random());
					System.out.println("broker id is :" + id);
					Long openAccountId = operate.createBrokerAccount(id + "");
					System.out.println("openAccountId:" + openAccountId);
					if (openAccountId == null) {
						logger.debug("注册阿里百川云账号失败");
					} else {
						user.setOpenAccountId(openAccountId);
						logger.debug("注册阿里百川云账号成功");
					}
					user.setIsvAccountId(id);

					brokerMapper.save(user);
					List<Broker> datas = new ArrayList<Broker>();
					datas.add(user);
					rb.setDatas(datas);
					rb.setCode(BrokerCons.LOGIN_SUCCESS);
					rb.setMessage(ReturnBean.SUCCESS_STR);
					return rb;
				}
				rb.setCode(BrokerCons.VALIDATE_FAIL);
				rb.setMessage("验证失败");
				return rb;
			}
			rb.setCode(BrokerCons.VALIDATE_FAIL);
			rb.setMessage("验证失败");
			return rb;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	public Map<String, Object> refresh(String devicenumber, int userid) {
		return null;
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.BrokerService#updateInfo(int, java.lang.String, int, java.lang.String)
	 */
	public int updateInfo(int sex, String username, int popid,
			String devicenumbe) {
		Broker broker = new Broker();
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("username", username);
		params.put("popid", popid);
		int countName = brokerMapper.getIsNameRepeat(params);
		int result =0;
		if (countName<1) {
			broker.setUsername(username);
			broker.setPopid(popid);
			broker.setSex(sex);
			broker.setUpdatetime(HelperUtil.getCurrentDate());
			result = brokerMapper.updateInfo(broker);
//			if (result>0) {
//				//获取最新信息
//				return brokerMapper.getInfo(popid);
//			}
		}else {
			return 2; //用户名重复
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.BrokerService#trained(int)
	 */
	public int trained(int brokerid) {
		return brokerMapper.trained(brokerid);
	}
	
	/* (non-Javadoc)
	 * @see me.send.broker.service.BrokerService#trained(int)
	 */
	public int tested(int brokerid) {
		return brokerMapper.tested(brokerid);
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.BrokerService#authen(me.send.broker.entity.Broker)
	 */
	public int authen(Broker broker) {
		return brokerMapper.authen(broker);
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.BrokerService#trainPics()
	 */
	public List<TrainPic> trainPics() {
		return brokerMapper.trainPics();
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.BrokerService#trainTests()
	 */
	public List<TrainTest> trainTests() {
		return brokerMapper.trainTests();
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.BrokerService#authenInfo(int)
	 */
	public Broker authenInfo(int brokerid) {
		return brokerMapper.authenInfo(brokerid);
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.BrokerService#getBrokerInfo(int)
	 */
	public BrokerIndexBean getBrokerInfo(int brokerid) {
		return brokerMapper.getInfo(brokerid);
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.BrokerService#feedback(int, java.lang.String, java.lang.String)
	 */
	public int feedback(Feedback feedback) {
		feedback.setCreatetime(HelperUtil.getCurrentDate());
		return brokerMapper.feedback(feedback);
	}

	/* (non-Javadoc)
	 * @see me.send.broker.service.BrokerService#isRegisted(java.lang.String)
	 */
	public int isRegisted(String telephone) {
		return brokerMapper.count_telephone(telephone);
	}
}
